//
//  BGViewladderpriceDataModel.m
//  bigMall
//
//  Created by LTOVE on 16/7/26.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGViewladderpriceDataModel.h"
#define textHeigh 18
@interface BGViewladderpriceDataModel (){
    UIView *_coverView;
    UIView *_alertView;
}

@end

@implementation BGViewladderpriceDataModel

//- (BGMessaGeItemOption)option

- (BGMessaGeItemOption)option
{
    void(^MYOption)(NSArray *) = ^(NSArray *dataArray){
        if (_dataArray.count == 0) {
            return ;//如果没有数据不弹窗
        }
        //创建蒙版视图
        [self setUpCoverView];
        //创建弹窗视图
        
//        NSArray *array = @[@"ddd",@"ddd",@"ddd",@"ddd",@"嘎嘎嘎",@"ddd"];
        
         [self setUpAlertWindowWithDataArray:_dataArray] ;
    };
    return MYOption;
}

- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray new];
    }
    return _dataArray;
}

/**
 *  增加弹窗视图
 */
- (void)setUpAlertWindowWithDataArray:(NSArray *)dataArray
{
    UIView *alertView = [[UIView alloc]init];
    _alertView = alertView;
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.alpha = 1;
    CGFloat width = _coverView.width - 60;
    if (dataArray) {
        CGFloat x = dataArray.count;
    }
    
    CGFloat heigh = dataArray.count == 0 ? BGMaginTop * 4 : BGMaginTop * 4 + (dataArray.count) * textHeigh;
    alertView.size = CGSizeMake(width, heigh);
    alertView.center = _coverView.center;
    //添加标题
    UILabel *titleLabel = [self iniTLabelWithStr:@"阶梯价格"];
    titleLabel.font  = Font34;
    titleLabel.textColor = textBlackColor;
    [titleLabel sizeToFit];
    titleLabel.point = CGPointMake(BGMaginLeft, BGMaginTop);
    
    [alertView addSubview:titleLabel];
    //添加我知道了
    UILabel *btnLabel = [self iniTLabelWithStr:@"我知道了"];
    btnLabel.textColor = GWColor(0, 124, 242, 1);
    btnLabel.font = Font30;
    [btnLabel sizeToFit];
    btnLabel.point = CGPointMake(alertView.width -  BGMaginRight - btnLabel.width, alertView.height - BGMaginButtom - btnLabel.height);
    [alertView addSubview:btnLabel];
    //动态添加数据
//    [self dynamicAddLabelsWith:alertView andDataArray:dataArray];
    [self dynamicAddLabelsWithFirstY:CGRectGetMaxY(titleLabel.frame) andDataArray:dataArray];
//    [_coverView addSubview:alertView];
    
    alertView.layer.cornerRadius = 7;
    alertView.layer.masksToBounds = YES;
    [BGKeyWindow addSubview:alertView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverViewTouched)];
    [alertView addGestureRecognizer:tap];
}
/**
 *  动态创建label
 *
 *  @param alertView alertView description
 *  @param dataArray dataArray description
 */
- (void)dynamicAddLabelsWithFirstY:(CGFloat )firstY andDataArray:(NSArray *)dataArray
{
    CGFloat y = firstY;
    for (int i = 0; i < dataArray.count; i ++) {
        y = i == 0 ? y + 10 : y +  textHeigh;
        UILabel * label = [self iniTLabelWithStr:dataArray[i]];
        label.font = Font30;
        label.textColor = textBlackColor;
        [label sizeToFit];
        label.point = CGPointMake(BGMaginLeft, y);
        [_alertView addSubview:label];
    }
}

/**
 *  初试化label
 *
 *  @param str str description
 *
 *  @return return value description
 */
- (UILabel *)iniTLabelWithStr:(NSString *)str
{
    UILabel *label = [[UILabel alloc]init];
    label.text = str;
    label.font = BGContenttextSize;
    label.textColor = [UIColor blackColor];
    [label sizeToFit];
    return label;
}

/**
 *  初始化蒙版视图
 */
- (void)setUpCoverView
{
    UIView *CoverView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    CoverView.backgroundColor = [UIColor blackColor];
    CoverView.alpha = 0.6;
    [BGKeyWindow addSubview:CoverView];
    _coverView = CoverView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverViewTouched)];
    [CoverView addGestureRecognizer:tap];
}
//蒙版点击事件
- (void)coverViewTouched
{
    [_coverView removeFromSuperview];
    [_alertView removeFromSuperview];
}

@end
